/**
 * com.cnhis.his.customer.service.Bbx1Service.java
 */
package com.mbmed.doctor.medadvice.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;




import com.mbmed.doctor.commons.mapper.ModelVo;
import com.mbmed.doctor.login.bo.Bck1Bo;
import com.mbmed.doctor.login.mapper.BckSub1;
import com.mbmed.doctor.medadvice.bo.Bbx1Bo;
import com.mbmed.doctor.medadvice.mapper.Bbx1;
import com.mbmed.doctor.medadvice.mapper.BbxSub1;

/**
 * @file Bbx1Service.java
 * @author 刘武
 * @version 0.1
 * @todo Bbx1服务操作接口 Copyright(C), 2013-2014 Shenzhen Coordinates Software
 *       Development Co., Ltd. History 1. Date: 2014-10-27 10:47:49 Author: 刘武
 *       Modification: this file was created 2. ...
 */
@Path("/bbx1")
@Component
public class Bbx1Service {

	private static Logger logger = Logger.getLogger(Bbx1.class);

	@Autowired
	private Bbx1Bo bbx1Bo;

	@Autowired
	private Bck1Bo bck1Bo;
	@Context
	Request request;

	/**
	 * 查找所有数据库记录
	 * 
	 * @return type : List 返回查询操作所有符合条件的记录的VO对象集合，操作失败返回null
	 */
	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public List<Bbx1> getAll() throws Exception {
		List<Bbx1> list = new ArrayList<Bbx1>();
		return bbx1Bo.getAll();
	}

	/**
	 * 查找符合条件的所有数据库记录
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Bbx1 getBbx1(@PathParam("id") String id) throws Exception {
		Bbx1 bbx1 = null;
		return bbx1Bo.get(id);
	}

	/**
	 * 检索药品
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivcesearchmedic")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivceSearchMedic(
			@QueryParam("acf01") java.lang.String acf01,
			@QueryParam("bdo01") java.lang.String bdo01,
			@QueryParam("sql") java.lang.String sql,
			@QueryParam("zbda01") java.lang.String zbda01,
			@QueryParam("xbda01") java.lang.String xbda01,
			@QueryParam("bck01") java.lang.String bck01,
			@QueryParam("bckyf") java.lang.String bckyf,
			@QueryParam("abc02") java.lang.String abc02) throws Exception {
		ModelVo modelvo = new ModelVo();

		int lacf01 = Integer.parseInt(acf01);
		int lbdo01 = Integer.parseInt(bdo01);
		String lsql = sql;
		int lbck01 = Integer.parseInt(bck01);
		int lbckyf = Integer.parseInt(bckyf);

		Map<String, Object> paramap = new HashMap<String, Object>();
		List<Bbx1> list = new ArrayList<Bbx1>();
		List<Bbx1> xcyList = new ArrayList<Bbx1>();
		List<Bbx1> zcyList = new ArrayList<Bbx1>();

		paramap.put("lacf01", lacf01);
		paramap.put("lbdo01", lbdo01);
		paramap.put("lsql", lsql);
		paramap.put("bda01", xbda01);
		paramap.put("lbck01", lbck01);
		paramap.put("lbckyf", lbckyf);
		paramap.put("abc02", abc02);

		xcyList = bbx1Bo.getAdivceSearchMedic(paramap);
		paramap.put("bda01", zbda01);
		zcyList = bbx1Bo.getAdivceSearchMedic(paramap);

		if (xcyList.size() > 0) {
			list.addAll(xcyList);
		}
		if (zcyList.size() > 0) {
			list.addAll(zcyList);
		}

		modelvo.setList(list);

		return modelvo;
	}

	/**
	 * 根据不同分类检索数据
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivcechoosemedic")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivceChooseMedic(
			@QueryParam("acf01") java.lang.String acf01,
			@QueryParam("bdo01") java.lang.String bdo01,
			@QueryParam("sql") java.lang.String sql,
			@QueryParam("bda01") java.lang.String bda01,
			@QueryParam("bck01") java.lang.String bck01,
			@QueryParam("bckyf") java.lang.String bckyf,
			@QueryParam("abc02") java.lang.String abc02,
			@QueryParam("starid") java.lang.String starid,
			@QueryParam("endid") java.lang.String endid) throws Exception {
		ModelVo modelvo = new ModelVo();

		int lacf01 = Integer.parseInt(acf01);
		int lbdo01 = Integer.parseInt(bdo01);
		String lsql = sql;
		int lbck01 = Integer.parseInt(bck01);
		int lbckyf = Integer.parseInt(bckyf);

		Map<String, Object> paramap = new HashMap<String, Object>();
		List<Bbx1> list = new ArrayList<Bbx1>();

		paramap.put("lacf01", lacf01);
		paramap.put("lbdo01", lbdo01);
		paramap.put("lsql", lsql);
		paramap.put("bda01", bda01);
		paramap.put("lbck01", lbck01);
		paramap.put("lbckyf", lbckyf);
		paramap.put("abc02", abc02);
		list = bbx1Bo.getAdivceSearchMedic(paramap);

		if ("null".equals(starid) || null == starid) {
			modelvo.setList(list);
		} else {

			int dataSize = list.size();
			List<Bbx1> pageList = null;

			int star = Integer.parseInt(starid);
			int end = Integer.parseInt(endid);
			if (end < dataSize) {
				pageList = list.subList(star, end);
			} else {
				pageList = list.subList(star, dataSize);
			}
			modelvo.setList(pageList);
			modelvo.setResult(dataSize + "");
		}
		return modelvo;
	}
	
	
	
	
	
	/**
	 * 根据不同分类检索数据
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getmediccommonproject")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getMedicCommonProject(
			@QueryParam("acf01") java.lang.String acf01,
			@QueryParam("bdo01") java.lang.String bdo01,
			@QueryParam("sql") java.lang.String sql,
			@QueryParam("bda01") java.lang.String bda01,
			@QueryParam("bck01") java.lang.String bck01,
			@QueryParam("bckyf") java.lang.String bckyf,
			@QueryParam("abc02") java.lang.String abc02,
			@QueryParam("bbx01") java.lang.String bbx01
			) throws Exception {
		ModelVo modelvo = new ModelVo();

		int lacf01 = Integer.parseInt(acf01);
		int lbdo01 = Integer.parseInt(bdo01);
		String lsql = sql;
		int lbck01 = Integer.parseInt(bck01);
		int lbckyf = Integer.parseInt(bckyf);
		int lbbx01 = Integer.parseInt(bbx01);

		Map<String, Object> paramap = new HashMap<String, Object>();
		List<Bbx1> list = new ArrayList<Bbx1>();

		paramap.put("lacf01", lacf01);
		paramap.put("lbdo01", lbdo01);
		paramap.put("lsql", lsql);
		paramap.put("bda01", bda01);
		paramap.put("lbck01", lbck01);
		paramap.put("lbckyf", lbckyf);
		paramap.put("abc02", abc02);
		paramap.put("lbbx01", lbbx01);
		list = bbx1Bo.getMedicCommonProject(paramap);
		List ls = new ArrayList();
		if(list.size()>0){
			ls.add(list.get(0));
		}
		modelvo.setList(ls);
		return modelvo;
	}
	

	/**
	 * 根据不同分类检索数据
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivcechooseproject")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivceSearchProject(
			@QueryParam("acf01") java.lang.String acf01,
			@QueryParam("bdo01") java.lang.String bdo01,
			@QueryParam("sql") java.lang.String sql,
			@QueryParam("bda01") java.lang.String bda01,
			@QueryParam("bck01") java.lang.String bck01,
			@QueryParam("bckyf") java.lang.String bckyf,
			@QueryParam("abc02") java.lang.String abc02,
			@QueryParam("bbx09") java.lang.String bbx09,
			@QueryParam("bce01") java.lang.String bce01,
			@QueryParam("starid") java.lang.String starid,
			@QueryParam("endid") java.lang.String endid) throws Exception {
		ModelVo modelvo = new ModelVo();

		int lacf01 = Integer.parseInt(acf01);
		int lbdo01 = Integer.parseInt(bdo01);
		String lsql = sql;
		int lbck01 = Integer.parseInt(bck01);
		int lbckyf = Integer.parseInt(bckyf);
		int lbbx09 = Integer.parseInt(bbx09);
		int lbce01 = Integer.parseInt(bce01);
		Map<String, Object> paramap = new HashMap<String, Object>();
		List<Bbx1> list = new ArrayList<Bbx1>();

		paramap.put("lacf01", lacf01);
		paramap.put("lbdo01", lbdo01);
		paramap.put("lsql", lsql);
		paramap.put("bda01", bda01);
		paramap.put("lbck01", lbck01);
		paramap.put("lbckyf", lbckyf);
		paramap.put("abc02", abc02);
		paramap.put("lbbx09", lbbx09);
		paramap.put("lbce01", lbce01);

		list = bbx1Bo.getAdivceSearchProject(paramap);
		if ("null".equals(starid) || null == starid) {
			modelvo.setList(list);
		} else {
			int dataSize = list.size();
			List<Bbx1> pageList = null;

			int star = Integer.parseInt(starid);
			int end = Integer.parseInt(endid);
			if (end < dataSize) {
				pageList = list.subList(star, end);
			} else {
				pageList = list.subList(star, dataSize);
			}
			modelvo.setList(pageList);
			modelvo.setResult(dataSize + "");
		}

		return modelvo;
	}

	/**
	 * 查询治疗方案常用列表
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivcecommonplan")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivceCommonPlan(
			@QueryParam("acf01") java.lang.String acf01,
			@QueryParam("bce01") java.lang.String bce01,
			@QueryParam("bck01") java.lang.String bck01,
			@QueryParam("starid") java.lang.String starid,
			@QueryParam("endid") java.lang.String endid) throws Exception {
		ModelVo modelvo = new ModelVo();

		int lacf01 = Integer.parseInt(acf01);
		int lbce01 = Integer.parseInt(bce01);
		int lbck01 = Integer.parseInt(bck01);
		Map<String, Object> paramap = new HashMap<String, Object>();
		List<Bbx1> list = new ArrayList<Bbx1>();

		paramap.put("lacf01", lacf01);
		paramap.put("lbce01", lbce01);
		paramap.put("lbck01", lbck01);

		list = bbx1Bo.getAdivceCommonPlan(paramap);
		if ("null".equals(starid) || null == starid) {
			modelvo.setList(list);
		} else {
			int dataSize = list.size();
			List<Bbx1> pageList = null;

			int star = Integer.parseInt(starid);
			int end = Integer.parseInt(endid);
			if (end < dataSize) {
				pageList = list.subList(star, end);
			} else {
				pageList = list.subList(star, dataSize);
			}
			modelvo.setList(pageList);
			modelvo.setResult(dataSize + "");
		}
		return modelvo;
	}

	/**
	 * 根据不同分类检索数据
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivceselectproject")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivceSelectProject(
			@QueryParam("acf01") java.lang.String acf01,
			@QueryParam("bdo01") java.lang.String bdo01,
			@QueryParam("sql") java.lang.String sql,
			@QueryParam("bch01") java.lang.String bch01,
			@QueryParam("bck01") java.lang.String bck01,
			@QueryParam("abc02") java.lang.String abc02,
			@QueryParam("bbx09") java.lang.String bbx09,
			@QueryParam("bce01") java.lang.String bce01,
			@QueryParam("starid") java.lang.String starid,
			@QueryParam("endid") java.lang.String endid) throws Exception {
		ModelVo modelvo = new ModelVo();

		int lacf01 = Integer.parseInt(acf01);
		int lbdo01 = Integer.parseInt(bdo01);
		String lsql = sql;
		String lbch01 = bch01;
		int lbck01 = Integer.parseInt(bck01);
		int lbbx09 = Integer.parseInt(bbx09);
		int lbce01 = Integer.parseInt(bce01);
		String labc02 = abc02;
		Map<String, Object> paramap = new HashMap<String, Object>();
		List<Bbx1> list = new ArrayList<Bbx1>();

		paramap.put("lacf01", lacf01);
		paramap.put("lbdo01", lbdo01);
		paramap.put("lsql", lsql);
		paramap.put("lbch01", lbch01);
		paramap.put("lbck01", lbck01);
		paramap.put("lbbx09", lbbx09);
		paramap.put("lbce01", lbce01);
		paramap.put("labc02", labc02);

		list = bbx1Bo.getAdivceSelectProject(paramap);
		if ("null".equals(starid) || null == starid) {
			modelvo.setList(list);
		} else {
			int dataSize = list.size();
			List<Bbx1> pageList = null;

			int star = Integer.parseInt(starid);
			int end = Integer.parseInt(endid);
			if (end < dataSize) {
				pageList = list.subList(star, end);
			} else {
				pageList = list.subList(star, dataSize);
			}
			modelvo.setList(pageList);
			modelvo.setResult(dataSize + "");
		}

		return modelvo;
	}

	/**
	 * 根据不同分类检索数据
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 返回查询操作所有符合条件的记录的VO对象，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivzcydecoctionmethod")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivZcyDecoctionMethod(
			@QueryParam("bda01") java.lang.String bda01,
			@QueryParam("bbx13") java.lang.String bbx13) throws Exception {
		ModelVo modelvo = new ModelVo();
		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("bda01", bda01);
		paramap.put("bbx13", bbx13);

		List<Bbx1> list = new ArrayList<Bbx1>();
		list = bbx1Bo.getAdivZcyDecoctionMethod(paramap);
		modelvo.setList(list);
		return modelvo;
	}

	/**
	 * 根据不同分类检索数据
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 检索给药执行，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivzcygivemethod")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivZcyGiveMethod(
			@QueryParam("text") java.lang.String text,
			@QueryParam("bda01") java.lang.String bda01,
			@QueryParam("bce01") java.lang.String bce01,
			@QueryParam("bbx13") java.lang.String bbx13) throws Exception {
		ModelVo modelvo = new ModelVo();
		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("text", text);
		paramap.put("bda01", bda01);
		paramap.put("bce01", bce01);
		paramap.put("bbx13", bbx13);

		List<Bbx1> list = new ArrayList<Bbx1>();
		list = bbx1Bo.getAdivGiveMethod(paramap);
		modelvo.setList(list);
		return modelvo;
	}

	/**
	 * 根据不同分类检索数据
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 药品：门诊按规格下医嘱，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivquerymedical")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivQueryMedical(
			@QueryParam("bby01") java.lang.String bby01) throws Exception {
		ModelVo modelVo = new ModelVo();
		Bbx1 bbx1 = new Bbx1();
		Map<String, Object> paramap = new HashMap<String, Object>();
		int bby01b = Integer.parseInt(bby01);
		paramap.put("bby01", bby01b);

		bbx1 = bbx1Bo.getAdivQueryMedical(paramap);
		Map<String, Object> remap = new HashMap<String, Object>();
		remap.put("bbx1", bbx1);
		modelVo.setMap(remap);
		return modelVo;
	}

	/**
	 * 查询附加手术
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 药品：门诊按规格下医嘱，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivsuboperate")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivSubOperate(
			@QueryParam("bda01") java.lang.String bda01,
			@QueryParam("starid") java.lang.String starid,
			@QueryParam("endid") java.lang.String endid) throws Exception {

		ModelVo modelvo = new ModelVo();
		List<Bbx1> list = new ArrayList<Bbx1>();
		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("bda01", bda01);
		list = bbx1Bo.getAdivSubOperate(paramap);

		if ("null".equals(starid) || null == starid) {
			modelvo.setList(list);
		} else {
			int dataSize = list.size();
			List<Bbx1> pageList = null;

			int star = Integer.parseInt(starid);
			int end = Integer.parseInt(endid);
			if (end < dataSize) {
				pageList = list.subList(star, end);
			} else {
				pageList = list.subList(star, dataSize);
			}
			modelvo.setList(pageList);
			modelvo.setResult(dataSize + "");
		}

		return modelvo;
	}

	/**
	 * 查询手术麻醉
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 药品：门诊按规格下医嘱，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getadivoperatenarcosis")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getAdivOperateNarcosis(
			@QueryParam("bda01") java.lang.String bda01) throws Exception {
		ModelVo modelVo = new ModelVo();
		List<Bbx1> bbx1list = new ArrayList<Bbx1>();
		Map<String, Object> paramap = new HashMap<String, Object>();
		Map<String, Object> paramap2 = new HashMap<String, Object>();
		paramap.put("bda01", bda01);
		bbx1list = bbx1Bo.getAdivOperateNarcosis(paramap);
		paramap2.put("bbx1list", bbx1list);
		modelVo.setMap(paramap2);

		return modelVo;
	}

	/**
	 * 执行方案查询
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 药品：门诊按规格下医嘱，操作失败返回null
	 * @throws Exception
	 */
	@POST
	@Path("/getadvicetreatmentplan")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo adviceStop(
			@FormParam("jobarlist") java.lang.String jobarlist,
			@FormParam("bck01c") java.lang.String bck01c,
			@FormParam("bck01d") java.lang.String bck01d,
			@Context HttpServletResponse servletResponse) throws Exception {
		ModelVo modelVo = new ModelVo();

		JSONArray josr = new JSONArray(jobarlist);

		// StringBuilder strBf = new StringBuilder();

		// strBf.append("<Root><BBX1>");
		// for(int i=0;i<josr.length();i++){
		// JSONObject job = josr.getJSONObject(i);
		// strBf.append("<Ie");
		// strBf.append(" BBX01B=\""+job.getString("bbx01b")+"\"");
		// strBf.append(" BEF01=\""+job.getString("bef01")+"\"");
		// strBf.append(" BBX01C=\""+job.getString("bbx01c")+"\"");
		// strBf.append(" BBY01=\""+job.getString("bby01")+"\"");
		// strBf.append("/>");
		// }
		// strBf.append("</BBX1></Root>");

		// String xml = strBf.toString();



		List<BbxSub1> bbx1list = new ArrayList<BbxSub1>();// 治疗方案：对应草药、手术明细
		
		List<BbxSub1> bbx1Set = new ArrayList<BbxSub1>();//治疗方案集合
		

		List<Object> list = new ArrayList<Object>();// 返回值结果集
		//
		Map<String, Object> valueMap = null;// 返回值元素封装
		
		Map<String,Object> bblsMap = new HashMap<String,Object>();
		
		//查询集合数据
		List<Integer> liBu0 = new ArrayList<Integer>();
		List<Integer> liBu1 = new ArrayList<Integer>();
		
		for (int i = 0; i < josr.length(); i++) {
			JSONObject job = josr.getJSONObject(i);
			int bbx01b = job.getInt("bbx01b");
			if (0==bbx01b) {
				liBu0.add(job.getInt("bef01"));
			} else {
				liBu1.add(bbx01b);
			}
		}
		if(liBu0.size()==0){
			liBu0.add(-1);
		}
		if(liBu1.size()==0){
			liBu1.add(-1);
		}
		Map<String,Object> lsMap = new HashMap<String,Object>();
		lsMap.put("acf01", "2");
		lsMap.put("strbx", liBu0);
		lsMap.put("strby", liBu1);
		bbx1Set = bbx1Bo.getTreatmentSet(lsMap);
		
		
		StringBuilder strBf = new StringBuilder();
		strBf.append("<Root><BBX1>");

		for (int i = 0; i < josr.length(); i++) {
			JSONObject job = josr.getJSONObject(i);

			String bbx01b = job.getString("bbx01b");
			String bef01 = null;
			if ("0".equals(bbx01b)) {
				bef01 = job.getString("bef01");
				bbx1list = getBbx1list(bef01,bbx1Set);
			} else {
				bbx1list = getBbx1list(bbx01b,bbx1Set);
			}

			// list.add(bbx1list);
			// }

			if (bbx1list.size() == 0) {
				strBf.append("<Ie");
				strBf.append(" LISIZE=\"" + 0 + "\"");
				strBf.append(" BEF01=\"" + job.getString("bef01") + "\"");
				strBf.append(" BBX01C=\"" + job.getString("bbx01c") + "\"");
				strBf.append(" BBY01=\"" + 0 + "\"");
				strBf.append("/>");
			} else {
				Iterator<BbxSub1> bbxIt = bbx1list.iterator();
				while (bbxIt.hasNext()) {
					BbxSub1 bbx1 = (BbxSub1) bbxIt.next();
					strBf.append("<Ie");
					strBf.append(" LISIZE=\"" + 1 + "\"");
					strBf.append(" BEF01=\"" + job.getString("bef01") + "\"");
					strBf.append(" BBX01C=\"" + bbx1.getBbx01() + "\"");

					if ("0".equals(bbx01b)) {
						strBf.append(" BBY01=\"" + bbx1.getBby01() + "\"");
					} else {
						strBf.append(" BBY01=\"" + job.getString("bby01")
								+ "\"");
					}

					strBf.append("/>");
				}
			}
			
			
			
			bblsMap.put(job.getString("bef01"), bbx1list);
		

			// if(bbx1list.size()==0){
			// //没值
			// valueMap = new HashMap<String,Object>();
			//
			// String bbx01c = job.getString("bbx01c");
			// diaTreBck = new HashMap<String,Object>();
			// diaTreBck.put("bck01", Integer.parseInt(bck01d));
			// diaTreBck.put("bbx01", Integer.parseInt(bbx01c));
			// diaTreBck.put("acf01", 2);
			//
			//
			// execbck1List = new ArrayList<Object>();
			//
			// bck1 = new BckSub1();
			// /* Description(获取诊疗项目对应执行科室) */
			// bck1 = bck1Bo.getDiagnosisTreatment(diaTreBck);
			//
			// if(null==bck1){
			// bck1 = bck1Bo.getAdivDefaultBck1(diaTreBck);
			// }
			// execbck1List.add(bck1);
			// medicbck1List = new ArrayList<Object>();
			// valueMap.put("diatre", bbx1list);
			// valueMap.put("execbck1", execbck1List);
			// valueMap.put("mdicbck", medicbck1List);
			// }
			// else{
			// //有值
			// valueMap = new HashMap<String,Object>();
			//
			// // BckSub1 execBck1 = bck1Bo.get(bck01c);
			//
			// mdicMap = new HashMap<String,Object>();
			//
			// mdicMap.put("acf01", 2);
			// mdicMap.put("bck01", Integer.parseInt(bck01d));
			//
			// Iterator<BbxSub1> bbxIt = bbx1list.iterator();
			//
			// medicbck1List = new ArrayList<Object>();
			//
			// execbck1List = new ArrayList<Object>();
			//
			// while(bbxIt.hasNext()){
			//
			// BbxSub1 bbx1 = (BbxSub1)bbxIt.next();
			// if("0".equals(bbx01b)){
			// Integer bby01 = bbx1.getBby01();
			// mdicMap.put("bby01", Integer.valueOf(bby01));
			// }
			// else{
			// String bby01 = job.getString("bby01");
			// mdicMap.put("bby01", Integer.parseInt(bby01));
			// }
			//
			// medicBck1 = bck1Bo.getMdeicBck1(mdicMap);
			// medicbck1List.add(medicBck1);
			//
			// Integer bbx01 = bbx1.getBbx01();
			// mdicMap.put("bbx01",Integer.valueOf(bbx01));
			//
			// BckSub1 execbck1 =
			// (BckSub1)bck1Bo.getDiagnosisTreatment(mdicMap);
			//
			// if(null==execbck1){
			// execbck1 = bck1Bo.getAdivDefaultBck1(mdicMap);
			// }
			//
			// execbck1List.add(execbck1);
			//
			// }
			// valueMap.put("diatre", bbx1list);
			// valueMap.put("execbck1", execbck1List);
			// valueMap.put("mdicbck", medicbck1List);
			// }
			// list.add(valueMap);
		}

		strBf.append("</BBX1></Root>");
		Map<String, Object> treMap = new HashMap<String, Object>();
		treMap.put("lXml", strBf.toString());
		treMap.put("bck01d", bck01d);
		treMap.put("rList", "");
		String rList = bbx1Bo.execClinicGetTreatmentBck(treMap);
		Map<String, Object> TreatmentBckMap = TreatmentBckAnalyticalXml(rList);
		
		for (int i = 0; i < josr.length(); i++) {
			JSONObject job = josr.getJSONObject(i);
			String bef01 = job.getString("bef01");
			valueMap = new HashMap<String,Object>();
			valueMap.put("diatre", bblsMap.get(bef01));
			 Map<String, Object> maplist = (Map<String, Object>) TreatmentBckMap.get(bef01);
			 valueMap.put("execbck1", maplist.get("execbck1"));
			 valueMap.put("mdicbck", maplist.get("mdicbck"));
			list.add(valueMap);
		}
		
		modelVo.setList(list);

		return modelVo;
	}
	
	
	public List getBbx1list(String str,List<BbxSub1> list){
		List<BbxSub1> ls = new ArrayList<BbxSub1>();
		Iterator<BbxSub1> it = list.iterator();
		while (it.hasNext()) {
			BbxSub1 bs = it.next();
			if(str.equals(bs.getBbx01s()+"")){
				ls.add(bs);
			}
		}
		return ls;
	}
	

	public Map TreatmentBckAnalyticalXml(String str) {
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> maplist = null;
		List<Object> medicbck1List = null;// 药房
		List<Object> execbck1List = null;// 执行科室
		
		BckSub1 bs = null;
		
		Document doc = null;
		
		String tmpbef="-1";
		
		try {
			
			// 将字符串转为XML
			doc = DocumentHelper.parseText(str);
			// 获取根节点
			Element rootElt = doc.getRootElement();
//			// 拿到根节点的名称
//			System.out.println("根节点：" + rootElt.getName());
//			// 获取根节点下的子节点
			Iterator<Element> iter = rootElt.elementIterator("BCK");
			
			// 遍历head节点  
	          while (iter.hasNext()) {
	              Element recordEle = (Element) iter.next();  
	              String bef01 = recordEle.attributeValue("BEF01");
	              String lize = recordEle.attributeValue("LIZE");
	              if(!tmpbef.equals(bef01)){
	            	  maplist = new HashMap<>();
	            	  maplist.put("execbck1", execbck1List);
	            	  maplist.put("mdicbck", medicbck1List);
	            	  map.put(tmpbef, maplist);
	            	  medicbck1List = new ArrayList<Object>();
	            	  execbck1List = new ArrayList<Object>();
	              }
	              // 获取子节点
	              Element elMIBCK = recordEle.element("MIBCK");
	              Element elEXBCK = recordEle.element("EXBCK");
	              if(null!=elEXBCK){
	            	  bs = new BckSub1();
	            	  bs.setBck01(Integer.parseInt(elEXBCK.attributeValue("BCK01")));
	            	  bs.setBck03(elEXBCK.attributeValue("BCK03"));
	            	  execbck1List.add(bs);
	              }
	              else{
	            	  if(!"0".equals(lize)){
	            		  execbck1List.add(null);
	            	  }
	              }
	              if(null!=elMIBCK){
	            	  bs = new BckSub1();
	            	  bs.setBck01(Integer.parseInt(elMIBCK.attributeValue("BCK01")));
	            	  bs.setBck03(elMIBCK.attributeValue("BCK03"));
	            	  medicbck1List.add(bs);
	              }
	              else{
	            	  medicbck1List.add(null);
	              }
	            
	              tmpbef = bef01;
	          }
	          
	          maplist = new HashMap<String,Object>();
        	  maplist.put("execbck1", execbck1List);
        	  maplist.put("mdicbck", medicbck1List);
        	  map.put(tmpbef, maplist);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 查询过敏药物
	 * 
	 * @param id
	 *            与数据库中主键对应的值
	 * @return type : 药品：门诊按规格下医嘱，操作失败返回null
	 * @throws Exception
	 */
	@GET
	@Path("/getguominmedic")
	@Produces({ MediaType.APPLICATION_JSON })
	public ModelVo getGuoMinMedic(@QueryParam("txt") java.lang.String txt,
			@QueryParam("starid") java.lang.String starid,
			@QueryParam("endid") java.lang.String endid) throws Exception {
		ModelVo modelVo = new ModelVo();
		List<Bbx1> bbx1list = new ArrayList<Bbx1>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("txt", txt);
		map.put("starid", Integer.parseInt(starid));
		map.put("endid", Integer.parseInt(endid));
		bbx1list = bbx1Bo.getGuoMinMedic(map);
		modelVo.setList(bbx1list);

		Bbx1 bbx1 = bbx1Bo.getCountGuoMinMedic(txt);
		modelVo.setResult(bbx1.getCout() + "");

		return modelVo;
	}

	/**
	 * 向数据库中插入一条记录
	 * 
	 * @param vo
	 *            与数据库中记录对应的值对象
	 * @return type : 返回插入操作是否成功
	 * @throws Exception
	 */
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Bbx1 saveBbx1(@FormParam("bbx01") java.lang.Integer bbx01,
			@FormParam("bda01") java.lang.String bda01,
			@FormParam("bdo01") java.lang.Integer bdo01,
			@FormParam("bbx04") java.lang.String bbx04,
			@FormParam("bbx05") java.lang.String bbx05,
			@FormParam("bbx06") java.lang.String bbx06,
			@FormParam("bdg02") java.lang.String bdg02,
			@FormParam("bbx08") java.lang.Byte bbx08,
			@FormParam("bbx09") java.lang.Byte bbx09,
			@FormParam("abw01") java.lang.String abw01,
			@FormParam("bbx11") java.lang.Byte bbx11,
			@FormParam("bbx12") java.lang.Byte bbx12,
			@FormParam("bbx13") java.lang.String bbx13,
			@FormParam("bbx14") java.lang.Byte bbx14,
			@FormParam("bbx15") java.lang.Integer bbx15,
			@FormParam("acf01") java.lang.Byte acf01,
			@FormParam("bbx17") java.lang.Byte bbx17,
			@FormParam("bbx18") java.math.BigDecimal bbx18,
			@FormParam("bbx19") java.lang.String bbx19,
			@FormParam("bbx20") java.lang.Integer bbx20,
			@FormParam("bbx21") java.lang.Byte bbx21,
			@FormParam("bcw01") java.lang.Integer bcw01,
			@FormParam("bce01") java.lang.Integer bce01,
			@FormParam("bbx24") java.util.Date bbx24,
			@FormParam("bbx25") java.util.Date bbx25,
			@FormParam("bbx26") java.lang.Byte bbx26,
			@FormParam("rownr") java.lang.Integer rownr,
			@FormParam("bbx28") java.lang.Byte bbx28,
			@FormParam("bbx29") java.lang.String bbx29,
			@FormParam("bbx30") java.lang.Byte bbx30,
			@FormParam("aas01") java.lang.String aas01,
			@FormParam("bbx32") java.lang.Byte bbx32,
			@FormParam("bce03a") java.lang.String bce03a,
			@FormParam("bbx35") java.util.Date bbx35,
			@FormParam("bbx33") java.lang.Byte bbx33,
			@FormParam("bbx36") java.lang.String bbx36,
			@FormParam("aah01") java.lang.String aah01,
			@FormParam("bbx38") java.math.BigDecimal bbx38,
			@FormParam("bbx39") java.lang.String bbx39,
			@FormParam("bbx40") java.lang.Byte bbx40,
			@FormParam("bbx41") java.lang.Integer bbx41,
			@FormParam("bbx42") java.lang.String bbx42,
			@Context HttpServletResponse servletResponse) throws Exception {
		Bbx1 bbx1 = new Bbx1();
		bbx1.setBbx01(bbx01);
		bbx1.setBda01(bda01);
		bbx1.setBdo01(bdo01);
		bbx1.setBbx04(bbx04);
		bbx1.setBbx05(bbx05);
		bbx1.setBbx06(bbx06);
		bbx1.setBdg02(bdg02);
		bbx1.setBbx08(bbx08);
		bbx1.setBbx09(bbx09);
		bbx1.setAbw01(abw01);
		bbx1.setBbx11(bbx11);
		bbx1.setBbx12(bbx12);
		bbx1.setBbx13(bbx13);
		bbx1.setBbx14(bbx14);
		bbx1.setBbx15(bbx15);
		bbx1.setAcf01(acf01);
		bbx1.setBbx17(bbx17);
		bbx1.setBbx18(bbx18);
		bbx1.setBbx19(bbx19);
		bbx1.setBbx20(bbx20);
		bbx1.setBbx21(bbx21);
		bbx1.setBcw01(bcw01);
		bbx1.setBce01(bce01);
		bbx1.setBbx24(bbx24);
		bbx1.setBbx25(bbx25);
		bbx1.setBbx26(bbx26);
		bbx1.setRownr(rownr);
		bbx1.setBbx28(bbx28);
		bbx1.setBbx29(bbx29);
		bbx1.setBbx30(bbx30);
		bbx1.setAas01(aas01);
		bbx1.setBbx32(bbx32);
		bbx1.setBce03a(bce03a);
		bbx1.setBbx35(bbx35);
		bbx1.setBbx33(bbx33);
		bbx1.setBbx36(bbx36);
		bbx1.setAah01(aah01);
		bbx1.setBbx38(bbx38);
		bbx1.setBbx39(bbx39);
		bbx1.setBbx40(bbx40);
		bbx1.setBbx41(bbx41);
		bbx1.setBbx42(bbx42);

		return bbx1 = bbx1Bo.save(bbx1);

	}

	/**
	 * 删除数据库中与传入的值对象对应的记录
	 * 
	 * @param vo
	 *            与数据库中记录对应的值对象
	 * @return type : boolean 返回删除操作是否成功
	 * @throws Exception
	 */
	@DELETE
	@Path("{id}")
	public boolean removeBbx1(@PathParam("id") String id) throws Exception {
		if (id == null)
			return false;
		return bbx1Bo.remove(id);

	}

}
